package com.example.maple.ccsclient.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.nineoldandroids.view.ViewHelper;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * Created by maple on 2016/8/13.
 */
public class JazzyViewPager extends ViewPager {

	private float mTrans;
	private float mScale;

	private boolean isMoving = false;

	//最大的缩放比例
	private static final float SCALE_MAX = 0.5f;

	/**
	 * 保存position与对于的View
	 */
	private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();

	//滑动时候左边的元素
	private View mLeftView;
	//滑动时候右边的元素
	private View mRightView;


	OnClickListener mOnClickListener;

	OnPagerSliding mOnPagerSliding;

	@Override
	public void setOnClickListener(OnClickListener l) {
		mOnClickListener = l;
	}

	public JazzyViewPager(Context context) {

		this(context, null);
	}

	public JazzyViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);

	}


	public void setOnPagerSilding(OnPagerSliding silding){
		mOnPagerSliding = silding;
	}

	private boolean isSmall(float positionOffset) {
		return Math.abs(positionOffset) < 0.0001;
	}

	public void setObjectForPosition(View simpleDraweeView, int position) {
		mChildrenViews.put(position, simpleDraweeView);
	}


	@Override
	protected void onPageScrolled(int position, float offset, int offsetPixels) {
		//滑动特别小的距离时，我们认为没有动，可有可无的判断
		float effectOffset = isSmall(offset) ? 0 : offset;

		//获取左边的View
		mLeftView = findViewFromObject(position);
		//获取右边的View
		mRightView = findViewFromObject(position + 1);

		// 添加切换动画效果
		animateStack(mLeftView, mRightView, effectOffset, offsetPixels);
		mOnPagerSliding.sliding(position);
		super.onPageScrolled(position, offset, offsetPixels);
	}

	private void animateStack(View leftView, View rightView, float effectOffset, int offsetPixels) {
		if (mRightView != null) {
			/**
			 * 缩小比例 如果手指从右到左的滑动（切换到后一个）：0.0~1.0，即从一半到最大
			 * 如果手指从左到右的滑动（切换到前一个）：1.0~0，即从最大到一半
			 */
			mScale = (1 - SCALE_MAX) * effectOffset + SCALE_MAX;
			/**
			 * x偏移量： 如果手指从右到左的滑动（切换到后一个）：0-720 如果手指从左到右的滑动（切换到前一个）：720-0
			 */
			mTrans = -getWidth() - getPageMargin() + offsetPixels;
			ViewHelper.setScaleX(mRightView, mScale);
			ViewHelper.setScaleY(mRightView, mScale);
			ViewHelper.setTranslationX(mRightView, mTrans);
		}
		if (mLeftView != null) {
			mLeftView.bringToFront();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {

		switch (ev.getAction()){

			case MotionEvent.ACTION_DOWN:{
				isMoving = false;
				break;
			}

			case MotionEvent.ACTION_MOVE:{
				isMoving = true;
				break;
			}

			case MotionEvent.ACTION_UP:{
				if(!isMoving){
					mOnClickListener.onClick(this);
				}
				break;
			}

			default:break;

		}

		return super.onTouchEvent(ev);
	}

	public View findViewFromObject(int position) {
		return mChildrenViews.get(position);
	}
}
